package com.yanqu.road.server.cmd.activity.gourdchild;

import com.yanqu.road.entity.activity.gourdchild.GourdChildNotice;
import com.yanqu.road.pb.activity.GourdChildProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.gourdchild.CrossGourdChildMgr;
import com.yanqu.road.server.manager.gourdchild.GourdChildActivity;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = CrossProtocol.C_GOURD_CHILD_ACTIVITY_GET_NOTICE, desc = "公告")
public class CrossGourdChildGetNoticeCmd extends CrossCommand {

    private int activityId;
    private long serverId;
    private long userId;

    private List<GourdChildNotice> noticeList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        GourdChildProto.CrossGetGourdChildNoticeReqMsg reqMsg = GourdChildProto.CrossGetGourdChildNoticeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        GourdChildActivity activity = CrossGourdChildMgr.getGourdChildActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_GOURD_CHILD_ACTIVITY_NO_OPEN;
            return;
        }

        noticeList = activity.getNoticeData();
    }

    @Override
    public void sendData() {
        GourdChildProto.GetGourdChildNoticeRespMsg.Builder builder = GourdChildProto.GetGourdChildNoticeRespMsg.newBuilder();
        builder.setRet(ret);
        for (GourdChildNotice notice : noticeList) {
            builder.addNoticeList(CrossGourdChildMgr.parseNoticeMsg(notice));
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(ClientProtocol.U_GOURD_CHILD_DRAW_NOTICE, builder));
    }
}
